﻿using Resn.WhuPub.BLL;
using Resn.WhuPub.DAL;
using Resn.WhuPub.DML;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Resn.WhuPub.BLL
{
        /// <summary>
        /// 实现联系人的属性和方法
        /// </summary>
        public class Contact :Entity<ContactModel>
        {
                #region 构造函数

                /// <summary>
                /// Initializes a new instance of the <see cref="Contact"/> class.
                /// </summary>
                /// <param name="model">The model.</param>
                public Contact(ContactModel model)
                        : base(model)
                {
                }

                #endregion


                #region 属性

                #region 基本属性

                

                /// <summary>
                /// 获取联系人所属部门部门Id
                /// </summary>
                /// <value>
                /// 联系人所属部门部门Id
                /// </value>
                public string DepartmentId
                {
                        get
                        {
                                return Model.DepartmentId;
                        }
                }

                /// <summary>
                /// 获取或设置联系人姓名
                /// </summary>
                /// <value>
                /// 联系人姓名
                /// </value>
                public string Name
                {
                        get
                        {
                                return Model.Name;
                        }
                        set
                        {
                                Model.Name = value;
                        }
                }

                /// <summary>
                /// 获取或设置联系人职称
                /// </summary>
                /// <value>
                /// 联系人职称，多个职称以分号隔开
                /// </value>
                public string Title
                {
                        get
                        {
                                return Model.Title;
                        }
                        set
                        {
                                Model.Title = value;
                        }
                }

                /// <summary>
                /// 获取或设置联系人职务
                /// </summary>
                /// <value>
                /// 联系人职务，多个职务已分号隔开
                /// </value>
                public string Duty
                {
                        get
                        {
                                return Model.Duty;
                        }
                        set
                        {
                                Model.Duty = value;
                        }
                }

                /// <summary>
                /// 获取或设置联系人电话
                /// </summary>
                /// <value>
                /// 联系人电话，多个电话以分号隔开
                /// </value>
                public string Telephone
                {
                        get
                        {
                                return Model.Telephone;
                        }
                        set
                        {
                                Model.Telephone = value;
                        }
                }

                /// <summary>
                /// 获取或设置联系人QQ
                /// </summary>
                /// <value>
                /// 联系人QQ，多个QQ以分号隔开
                /// </value>
                public string QQ
                {
                        get
                        {
                                return Model.QQ;
                        }
                        set
                        {
                                Model.QQ = value;
                        }
                }

                /// <summary>
                /// 获取或设置联系人电子邮箱
                /// </summary>
                /// <value>
                /// 联系人电子邮箱，多个电子邮箱以分号隔开
                /// </value>
                public string Email
                {
                        get
                        {
                                return Model.Email;
                        }
                        set
                        {
                                Model.Email = value;
                        }
                }

                /// <summary>
                /// 获取或设置联系人是否为本社作者
                /// </summary>
                /// <value>
                /// 联系人为本社作者时为True,否则为False
                /// </value>
                public bool IsOwnAuthor
                {
                        get
                        {
                                return Model.IsOwnAuthor;
                        }
                        set
                        {
                                Model.IsOwnAuthor = value;
                        }
                }

                /// <summary>
                /// 获取或设置联系人社会兼职
                /// </summary>
                /// <value>
                /// 联系人社追兼职，多个兼职以分号隔开
                /// </value>
                public string SoicalDuty
                {
                        get
                        {
                                return Model.SoicalDuty;
                        }
                        set
                        {
                                Model.SoicalDuty = value;
                        }
                }

                /// <summary>
                /// 获取或设置备注
                /// </summary>
                /// <value>
                /// 备注
                /// </value>
                public string Memo
                {
                        get
                        {
                                return Model.Memo;
                        }
                        set
                        {
                                Model.Memo = value;
                        }
                }

                

                #endregion 基本属性

                

                /// <summary>
                /// 获取联系人所属的学校/公司
                /// </summary>
                /// <value>
                /// 联系人所属的学校/公司
                /// </value>
                public Corporate Corporate
                {
                        get
                        {
                                return Branch.Corporate;
                        }
                }

                private Branch _corporateBranch;

                /// <summary>
                /// 获取联系人所属的校区校/分公司
                /// </summary>
                /// <value>
                /// 联系人所属的校区/分公司
                /// </value>
                public Branch Branch
                {
                        get { return _corporateBranch ?? (_corporateBranch = Department.Branch); }
                }

                /// <summary>
                /// 获取联系人所属的部门
                /// </summary>
                /// <value>
                /// 联系人所属的部门
                /// </value>
                public Department Department
                {
                        get
                        {
                                var model = DBStorage.Instance.Select<DepartmentModel>(DepartmentId);
                                return new Department(model);
                        }
                }

                /// <summary>
                /// 获取学历
                /// </summary>
                /// <value>
                /// 学历项集合
                /// </value>
                public List<Educational> Educationals
                {
                        get
                        {
                                var models = DBStorage.Instance.Select<EducationalModel>().Where(i => i.ContactId == Id).ToList();

                                var result = new List<Educational>();

                                models.ForEach(i => result.Add(new Educational(i)));

                                return result;
                        }
                }

                /// <summary>
                /// 获取联系人是否为一名教师
                /// </summary>
                /// <value>
                /// True：是教师
                /// False：不是教师
                /// </value>
                public bool IsTeacher
                {
                        get
                        {
                                if (Corporate.CorporateLevel == CorporateLevel.公司)
                                {
                                        return false;
                                }
                                return true;
                        }
                }

                /// <summary>
                /// 获取联系人的书籍集合
                /// </summary>
                /// <value>
                /// 联系人的书籍集合
                /// </value>
                public List<Book> Books
                {
                        get
                        {
                                var models = DBStorage.Instance.Select<BookModel>().Where(i => i.ContactId == Id).ToList();

                                var result = new List<Book>();

                                models.ForEach(i => result.Add(new Book(i)));

                                return result;
                        }
                }

                /// <summary>
                /// 获取联系人的反馈集合
                /// </summary>
                /// <value>
                /// 联系人的反馈集合
                /// </value>
                public List<FeedbackFlow> FeedbackFlows
                {
                        get
                        {
                                var models = DBStorage.Instance.Select<FeedbackFlowModel>().Where(i => i.ContactId == Id).ToList();

                                var result = new List<FeedbackFlow>();

                                models.ForEach(i => result.Add(new FeedbackFlow(i)));

                                return result;
                        }
                }

                /// <summary>
                /// 获取联系人的样书集合
                /// </summary>
                /// <value>
                /// 联系人的样书集合
                /// </value>
                public List<BookFlow> BookFlows
                {
                        get
                        {
                                var models = DBStorage.Instance.Select<BookFlowModel>().Where(i => i.ContactId == Id).ToList();

                                var result = new List<BookFlow>();

                                models.ForEach(i => result.Add(new BookFlow(i)));

                                return result;
                        }
                }

                #endregion 属性

                #region 方法

                /// <summary>
                /// 获取指定标识的对象
                /// </summary>
                /// <param name="id">标识</param>
                /// <returns></returns>
                public static Contact Select(string id)
                {
                        return Select<Contact>(id);
                }

                /// <summary>
                /// 创建联系人
                /// </summary>
                /// <param name="departmentId">The department id.</param>
                /// <param name="name">名称</param>
                /// <param name="title">The title.</param>
                /// <param name="duty">The duty.</param>
                /// <param name="telephone">The telephone.</param>
                /// <param name="qq">The qq.</param>
                /// <param name="email">The email.</param>
                /// <param name="isOwnAuthor">if set to <c>true</c> [is own author].</param>
                /// <param name="memo">The memo.</param>
                /// <returns>
                /// 创建的联系人对象
                /// </returns>
                public static Contact CreateContact(string departmentId, string name, string title = "", string duty = "", string telephone = "",
                                       string qq = "", string email = "", bool isOwnAuthor = false, string memo = "")
                {
                        var model = new ContactModel
                        {
                                Id = Guid.NewGuid().ToString(),
                                Name = name,
                        };

                        return new Contact(model);
                }

                /// <summary>
                /// 创建学历
                /// </summary>
                /// <param name="graduateSchool">毕业院校</param>
                /// <param name="educationalLevel">学历层次</param>
                /// <param name="majorId">专业标识</param>
                /// <param name="startTime">入学时间</param>
                /// <param name="endTime">毕业时间</param>
                /// <returns>
                /// 创建的学历对象
                /// </returns>
                /// <exception cref="System.NotImplementedException"></exception>
                public Educational CreateEducational(string graduateSchool, EducationalLevel educationalLevel, string majorId, DateTime startTime, DateTime endTime)
                {
                        var model = new EducationalModel
                                {
                                        Id = Guid.NewGuid().ToString(),
                                        ContactId = Id,
                                        GraduateSchool = graduateSchool,
                                        EducationalLevel = educationalLevel.ToString(),
                                        MajorId = majorId,
                                        StartTime = startTime,
                                        EndTime = endTime
                                };

                        return new Educational(model);
                }

                

                /// <summary>
                /// 创建书籍
                /// </summary>
                /// <param name="name">名称</param>
                /// <param name="partakeType">编写方式</param>
                /// <param name="publisher">出版社</param>
                /// <returns>
                /// 创建的反馈对象
                /// </returns>
                public Book CreateBook(string name, PartakeType partakeType, string publisher)
                {
                        var model = new BookModel
                                {
                                        Id = Guid.NewGuid().ToString(), 
                                        ContactId = Id,
                                        Name = name,
                                        PartakeType = partakeType.ToString(),
                                        Publisher = publisher
                                };
                        return new Book(model);
                }

                

                /// <summary>
                /// 创建反馈
                /// </summary>
                /// <param name="feedbackType">反馈类型</param>
                /// <param name="feedbackContent">反馈内容</param>
                /// <param name="enteringEmployeeId">录入员工标识</param>
                /// <param name="approveEmployeeId">审批员工标识</param>
                /// <returns>
                /// 创建的反馈对象
                /// </returns>
                /// <exception cref="System.NotImplementedException"></exception>
                public FeedbackFlow CreateFeedbackFlow(FeedbackFlowType feedbackType, string feedbackContent, string enteringEmployeeId, string approveEmployeeId)
                {
                        var model = new FeedbackFlowModel
                                {
                                        Id = Guid.NewGuid().ToString(),
                                        ContactId = Id,
                                        FeedbackFlowType = feedbackType.ToString(),
                                        Content = feedbackContent,
                                        EnteringEmployeeId = enteringEmployeeId,
                                        EnteringTime = DateTime.Now,
                                        ApproveEmployeeId = approveEmployeeId,
                                        ApproveContent = string.Empty,
                                        ApproveTime = DateTime.MinValue
                                };

                        return new FeedbackFlow(model);
                }

                

                /// <summary>
                /// 创建样书寄送流程
                /// </summary>
                /// <param name="bookId">样书标识</param>
                /// <param name="count">数量</param>
                /// <param name="deliveryNumber">寄送单号</param>
                /// <param name="deliveryTime">寄送时间</param>
                /// <returns>
                /// 创建的样书寄送流程对象
                /// </returns>
                /// <exception cref="System.NotImplementedException"></exception>
                public BookFlow CreateBookFlow(string bookId, int count, string deliveryNumber, DateTime deliveryTime)
                {
                        throw new NotImplementedException();
                }


                #endregion 方法


                
        }
}